/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.core.domain.bank;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import info.peigen.hotpot.business.account.core.repository.entity.BankEntity;
import info.peigen.hotpot.common.core.domain.bank.Bank;
import info.peigen.hotpot.common.core.util.IdUtil;

/**
 * <b>(CardConvertor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/8/12
 */
public class BankConvertor {
    public static BankEntity convertToEntity(Bank domain) {
        BankEntity bankEntity = BeanUtil.copyProperties(domain, BankEntity.class).setBankId(IdUtil.id()).setBank(JSON.toJSONString(domain));
        if (ObjectUtil.isNotNull(domain.getTransCode()) && ObjectUtil.isNotNull(domain.getTransCode().getSwiftCode())) {
            bankEntity.setSwiftCode(domain.getTransCode().getSwiftCode().getSwiftCode());
        }
        return bankEntity;
    }

    public static void convertToEntity(Bank domain, BankEntity entity) {
        BeanUtil.copyProperties(domain, entity, CopyOptions.create().setIgnoreNullValue(true).setIgnoreProperties("address", "bank"));
        entity.setAddress(JSON.toJSONString(domain.getAddress()))
                .setBank(JSON.toJSONString(domain));
    }

    public static Bank convertToEntity(BankEntity entity) {
        return JSON.parseObject(entity.getBank(), Bank.class);
    }

    // ConvertToDomain
    public static Bank convertToDomain(BankEntity entity) {
        return BeanUtil.copyProperties(entity, Bank.class);
    }
}
